<template>
    <el-dialog
            :title="!dataForm.responsibilityId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="社会责任ID" prop="responsibilityId">
                        <el-input v-model.number="dataForm.responsibilityId" placeholder="社会责任ID"></el-input>
                </el-form-item>
                <el-form-item label="社会责任类型（energy_efficiency: 综合能效提升；renewable_energy_analysis: 可再生能源消纳分析）" prop="responsibilityType">
                        <el-input v-model="dataForm.responsibilityType" placeholder="社会责任类型（energy_efficiency: 综合能效提升；renewable_energy_analysis: 可再生能源消纳分析）"></el-input>
                </el-form-item>
                <el-form-item label="责任日期" prop="responsibilityDate">
                        <el-date-picker
                                v-model="dataForm.responsibilityDate"
                                type="datetime"
                                placeholder="责任日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="责任结果" prop="responsibilityResult">
                        <el-input v-model="dataForm.responsibilityResult" placeholder="责任结果"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            responsibilityId: null,
            responsibilityType: null,
            responsibilityDate: null,
            createdAt: null,
            updatedAt: null,
            responsibilityResult: null
          },
          dataRule: {
            responsibilityId: [
                        { required: true, message: '社会责任ID不能为空', trigger: 'blur' }
            ],
            responsibilityType: [
                        { required: true, message: '社会责任类型（energy_efficiency: 综合能效提升；renewable_energy_analysis: 可再生能源消纳分析）不能为空', trigger: 'blur' }
            ],
            responsibilityDate: [
                        { required: true, message: '责任日期不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ],
            responsibilityResult: [
                        { required: true, message: '责任结果不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.responsibilityId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.responsibilityId = id || 0
            if (this.dataForm.responsibilityId) {
              this.$http({
                url: this.$http.adornUrl(`/app/social_responsibility/info/${this.dataForm.responsibilityId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appsocialresponsibility
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/social_responsibility/${!this.dataForm.responsibilityId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>